using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using Common;
using Common.Enum;
using SExperiment;
using SProject;
using SProject.Paramter;
using SProject.Program;

namespace SWellPlate
{
    public partial class WellEditWindow : Window, IComponentConnector
    {
        private Experiment Experiment;

        private bool isFirstTime;

        public List<WellCell> SelectWells { get; set; }

        public Project ComboxSelectedProject { get; set; }

        public WellEditWindow(List<WellCell> list)
        {
            InitializeComponent();
            SelectWells = list;
            UCWellTargetEditor.SetSelectedSample(list.Select((WellCell s) => s.Well).ToList());
            UCWellTargetEditor.IsSetValue = false;
        }

        private void btnApply_Click(object sender, RoutedEventArgs e)
        {
            if (ComboxSelectedProject != null)
            {
                if (ComboxSelectedProject.IsEditing)
                {
                    MessageBox.Show(string.Format(Utility.GetMessage("msg_project_cannot_set"), ComboxSelectedProject.BasicInfo.Name), Utility.GetMessage("error"), MessageBoxButton.OK);
                    return;
                }
                if (Experiment.Type != EProjectType.AD && SelectWells.Count < ComboxSelectedProject.BasicOption.Items.Select((BasicOptionItem s) => s.TubeNo).Max())
                {
                    MessageBox.Show(Utility.GetMessage("msg_well_not_enough"), Utility.GetMessage("error"), MessageBoxButton.OK);
                    return;
                }
            }
            if (ComboxSelectedProject != null && Experiment.Program == null)
            {
                Experiment.Program = new PCRProgram(ComboxSelectedProject.PCRProgram);
            }
            if (ComboxSelectedProject == null && (Experiment.Status == EExperimentStatus.NotRunning || Experiment.Status == EExperimentStatus.Interrupt))
            {
                IList<Well> list = Experiment.Wells.Where((Well s) => s.Project != null).Distinct().ToList();
                if (list.Count == 0)
                {
                    Experiment.Program = null;
                }
            }
            base.DialogResult = true;
        }

        public void SetResult()
        {
            UCWellTargetEditor.SetResult();
        }

        public void IniProjectCombox(Experiment _Experiment, Project SelectedProject)
        {
            Experiment = _Experiment;
            UCWellTargetEditor.SetBinding(UCWellTargetEditor.ProjectTypeProperty, new Binding("Type")
            {
                Source = Experiment
            });
            IList<Project> list = new List<Project>();
            IList<Project> prjTypeList = (from s in _Experiment.Wells
                                          where s.Project != null && s.Project.BasicInfo.Type == _Experiment.Type
                                          select s.Project).Distinct().ToList();
            for (int i = 0; i < prjTypeList.Count; i++)
            {
                if (_Experiment.Program == null || prjTypeList[i].PCRProgram.Equals(_Experiment.Program))
                {
                    list.Add(prjTypeList[i]);
                }
            }
            prjTypeList = ProjectManage.GetInstance().AllProjectList.Where((Project s) => s.BasicInfo.Type == _Experiment.Type).ToList();
            int j;
            for (j = 0; j < prjTypeList.Count; j++)
            {
                if (list.Where((Project s) => s.BasicInfo.Name == prjTypeList[j].BasicInfo.Name).Count() == 0 && (_Experiment.Program == null || prjTypeList[j].PCRProgram.Equals(_Experiment.Program)) && _Experiment.ProjectChannelMatch(prjTypeList[j]))
                {
                    list.Add(prjTypeList[j]);
                }
            }
            comboAllProject.ItemsSource = list;
            foreach (Project item in list)
            {
                if (item == SelectedProject)
                {
                    isFirstTime = true;
                    comboAllProject.SelectedItem = item;
                    ComboxSelectedProject = item;
                }
            }
        }

        private void Button_delete_Click(object sender, RoutedEventArgs e)
        {
            comboAllProject.SelectedItem = null;
        }

        private void comboAllProject_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboAllProject.SelectedItem != null)
            {
                if (isFirstTime)
                {
                    isFirstTime = false;
                    return;
                }
                ComboxSelectedProject = (Project)comboAllProject.SelectedItem;
                Sample selectedSample = new Sample(ComboxSelectedProject, 0);
                UCWellTargetEditor.SetSelectedSample(selectedSample);
                UCWellTargetEditor.IsEnabled = true;
            }
            else
            {
                ComboxSelectedProject = null;
                UCWellTargetEditor.IsEnabled = false;
            }
        }

        [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
        [DebuggerNonUserCode]
        internal Delegate _CreateDelegate(Type delegateType, string handler)
        {
            return Delegate.CreateDelegate(delegateType, this, handler);
        }
    }
}
